home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / grafik / cgazv5n3 / ot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-25  |  3.7 KB  |  127 lines

  1. /************  Listing 1 *********************  OT.C  **********************
  2. *
  3. *  ot.c --   Object Record Traversal
  4. *  Author:   Thomas E. Siering, 1991
  5. *  Usage:    This utility will list the Record Types present in a given
  6. *            OBJ module.  Any command line parameters (other than the
  7. *            OBJ file name) are considered Record Types.  In that case,
  8. *            only the presence of those types will be reported.
  9. *            Any Record Type 0 <= RT <= 255 will be searched for even if
  10. *            it is not (universally) recognized as part of the OMF standard.
  11. *            CAVEAT: Record Type on command line has to be DECIMAL!
  12. *  Compiler: Microsoft C 6.0, Turbo C++ 1.0
  13. *  Compile time switches: none
  14. *  Links with: This is a stand-alone module.
  15. *
  16. *  (c) Copyright 1991, C Gazette. May be used freely as long as authorship
  17. *                      and publication are acknowledged.
  18. ***************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23.  
  24.  
  25. #pragma pack(1)         /* Byte-align the ObjHeader struct */
  26.  
  27. typedef enum { false, true } bool;
  28.  
  29.  
  30. static char ProgName[] = { "OT" };
  31.  
  32.  
  33. void main (int argc, char *argv[])
  34. {
  35.     FILE *ObjFH;
  36.     int RecTypeCount[UCHAR_MAX + 1];
  37.     bool DoShow[UCHAR_MAX + 1];
  38.  
  39.     struct {
  40.         unsigned char RecordType;
  41.         int RecordLength;
  42.     } ObjHeader;
  43.  
  44.     int RecTotal;
  45.     int i;
  46.  
  47.     /* Usage info for the uninitiated */
  48.     if (argc < 2) 
  49.     {
  50.         fprintf(stderr, "\aUsage: %s fn.obj [options]\n", ProgName);
  51.         fprintf(stderr, "Options: Record Types to list\n");
  52.         exit(-1);
  53.     }
  54.  
  55.     /* Check out the OBJ module file */
  56.     if ( (ObjFH = fopen(argv[1], "rb")) == NULL ) 
  57.     {
  58.         fprintf(stderr, "\aFile %s could not be opened\n", argv[1]);
  59.         exit(-1);
  60.     }
  61.  
  62.     /* So far, no instances of any Record Type have been sighted */
  63.     for (i = 0; i <= UCHAR_MAX; i++)
  64.         RecTypeCount[i] = 0;
  65.  
  66.     /* Sort out which OBJ records we'll want to know about */
  67.     if (argc == 2)
  68.         /* Show them all! */
  69.         for (i = 0; i <= UCHAR_MAX; i++)
  70.             DoShow[i] = true;
  71.     else 
  72.     {
  73.         int RecType;
  74.  
  75.         for (i = 0; i <= UCHAR_MAX; i++)
  76.             DoShow[i] = false;
  77.  
  78.         for (i = argc; i > 2; i--) 
  79.         {
  80.             RecType = atoi(argv[i - 1]);
  81.             if (RecType < 0 || RecType > UCHAR_MAX)
  82.                 fprintf(stderr, "\aIncorrect Record Type entered: %X\n",
  83.                         RecType);
  84.             else
  85.                 DoShow[RecType] = true;
  86.         }
  87.     }
  88.  
  89.  
  90.     while ( fread(&ObjHeader, sizeof(ObjHeader), 1, ObjFH) == 1) 
  91.     {
  92.         /* Another OBJ record to account for */
  93.         RecTypeCount[ObjHeader.RecordType]++;
  94.         fseek(ObjFH, (long) ObjHeader.RecordLength, SEEK_CUR);
  95.  
  96.     #ifdef VERBOSE
  97.         fprintf(stdout, "Record Type = %02X\nLength = %d\n",
  98.                 ObjHeader.RecordType & 0x00FF, ObjHeader.RecordLength);
  99.     #endif
  100.     }
  101.  
  102.     /* Make sure last read failed because of EOF. 
  103.        Otherwise there's trouble! */
  104.     if (feof (ObjFH) == 0) 
  105.     {
  106.         fprintf(stderr, "\aRead of OBJ Failed.  Aborting\n");
  107.         exit(-1);
  108.     }
  109.  
  110.     fclose(ObjFH);
  111.  
  112.     /* Finally, print the statistics */
  113.     fprintf(stdout, "File %s:\n", argv[1]);
  114.     for (i = 0, RecTotal = 0; i <= UCHAR_MAX; i++) 
  115.     {
  116.         if (RecTypeCount[i] > 0 && DoShow[i] == true) 
  117.         {
  118.             fprintf(stdout, "RecordType %X  --  %d\n", i, RecTypeCount[i]);
  119.             RecTotal += RecTypeCount[i];
  120.         }
  121.     }
  122.     fprintf(stdout, "Total Records: %d\n\n", RecTotal);
  123. }